<!-- 合同修改申请单 -->
<template>
  <div class="contractChangeApplicationManage">
    <el-card>
      <div class="opt_wrap">
        <div class="opt_wrap_left">
          <p>
            已选中<span style="color: #1796EB">{{ selectedNum }}</span
            >项
          </p>
        </div>
      </div>
      <statisticsTag
        v-if="statisticsTagData"
        :data="statisticsTagData"
        @searchStatus="searchStatus"
      />
      <div class="search_wrap">
        <el-input
          placeholder="学员名称/合同编号/学员手机号/负责人"
          v-model="searchCondition"
          clearable
          style="width: 260px;"
          @keyup.enter.native="queryListData"
        >
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
        </el-input>
        <el-date-picker
          v-model="queryDate"
          @change="queryListData"
          type="daterange"
          range-separator="~"
          start-placeholder="提交起始日期"
          end-placeholder="提交结束日期"
        >
        </el-date-picker>
        <el-select
          v-model="status"
          placeholder="合同状态"
          @change="queryListData"
          filterable
          clearable
        >
          <el-option
            v-for="item in statisticsTagData"
            :key="item.index"
            :label="item.label"
            :value="item.status"
          >
            {{ item.label }}
          </el-option>
        </el-select>
      </div>
      <div class="table_list">
        <el-table
          ref="nicheTable"
          :data="tableListData"
          min-height="500"
          border
          v-loading="loading"
          @selection-change="handleSelectionNiche"
          style="width: 100%"
        >
          <el-table-column type="selection" align="center" width="40" />
          <el-table-column
            v-for="(item, index) in tableCaption"
            :key="index"
            :label="item.label"
            align="center"
          >
            <template slot-scope="scope">
              <span>{{ scope.row[item.value] }}</span>
              <!-- <span v-if="item.value === 'name'" @click="goToURL('appointDetail', {type: 'see'})" class="see_detail">{{ scope.row[item.value] }}</span> -->
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <a
                class="operation"
                @click="
                  goToURL('contractChangeApplicationDetails', {
                    id: scope.row.id
                  })
                "
                >查看</a
              >
              <a
                class="operation"
                @click="examine(scope.row.id)"
                v-if="scope.row.status === '待审批'"
                >审核</a
              >
            </template>
          </el-table-column>
        </el-table>
        <link-pagination
          :total="total"
          :currentPage="currentPage"
          :limit="limit"
          v-if="total != 0"
          @pagination="getPaginationParams"
        ></link-pagination>
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      title="提示"
      width="510px"
      :visible.sync="showDelModal"
      class="del_modal"
    >
      <div class="content">
        <p><i class="iconfont icon-tishi"></i></p>
        <!-- <p v-if="selectionCustomer.length === 1">是否将客户<span>{{ selectionCustomer[0].name }}删除？</span></p>
        <p v-if="selectionCustomer.length > 1">是否将<span>{{ selectionCustomer[0].name }}、{{ selectionCustomer[1].name }}<span v-if="selectionCustomer.length > 2">等</span>{{ selectionCustomer.length }}位客户删除？</span></p> -->
        <p>删除成功之后，该操作将无法恢复</p>
      </div>
      <div class="btn">
        <el-button @click="showDelModal = false">返回</el-button>
        <el-button type="primary" @click="confirmDelete">确认</el-button>
      </div>
    </el-dialog>
    <!-- 审核弹框 -->
    <el-dialog
      :title="`修改${infoTittle}`"
      width="510px"
      :visible.sync="examineModal"
    >
      <div>
        <el-form
          ref="form"
          :model="examineForm"
          :rules="examineRules"
          label-position="left"
          label-width="100px"
          v-if="infoDetail"
        >
          <el-form-item
            v-for="item in infoDetail"
            :key="item.value"
            :label="`${item.label}`"
            prop="sno"
          >
            <p>{{ item.value }}</p>
          </el-form-item>
          <el-form-item label="备注">
            <p>{{ infoRemark }}</p>
          </el-form-item>
          <el-form-item label="审核" prop="status">
            <el-radio v-model="examineForm.status" label="1">通过</el-radio>
            <el-radio v-model="examineForm.status" label="2">驳回</el-radio>
          </el-form-item>
          <el-form-item label="原因" prop="audit_msg">
            <el-input
              type="textarea"
              :rows="3"
              placeholder="请输入内容"
              v-model="examineForm.audit_msg"
            >
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="btn">
        <el-button @click="examineModal = false">返回</el-button>
        <el-button type="primary" @click="confirmArrive">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import statisticsTag from "@/components/statisticsTag";
import { paginationMixin } from "@/mixin";
import http from "@/api/contract_audit";
import { formatDate, formatArray } from "@/utils";
export default {
  name: "contractChangeApplicationManage",
  mixins: [paginationMixin],
  components: {
    statisticsTag
  },
  data() {
    return {
      http: http,
      selectedNum: 0,
      showBtn: false,
      showDelModal: false,
      searchCondition: null,
      queryDate: null,
      step: null,
      steps: null,
      type: null,
      types: null,
      loading: false,
      examineModal: false,
      arriveFormData: {},
      arriveFormrules: {},
      total: 1,
      tableCaption: [
        {
          label: "变更类型",
          value: "varname"
        },
        {
          label: "原始合同",
          value: "contractNo"
        },
        {
          label: "学员",
          value: "name"
        },
        // {
        //   label: '学员类型',
        //   value: 'statusDec'
        // },
        {
          label: "审批状态",
          value: "status"
        },
        {
          label: "审批岗位",
          value: "role_id"
        },
        {
          label: "审批人",
          value: "username"
        },
        {
          label: "审批通过时间",
          value: "audit_time"
        },
        {
          label: "创建时间",
          value: "create_time"
        }
      ],
      tableListData: [],
      contractChangeStatusList: [],
      statisticsTagData: [],
      status: null,
      contractStatusList: [],
      auditStatusList: [],
      examineModal: false,
      infoTittle: null,
      infoDetail: [],
      examineForm: {},
      examineRules: {},
      infoRemark: "",
      detailInfo: null,
      backgroundStart: [
        "#17B5EB",
        "#F27450",
        "#ffc31f",
        "#a946eb",
        "#02c779",
        "#17B5EB",
        "#F27450",
        "#ffc31f",
        "#a946eb",
        "#02c779"
      ],
      backgroundEnd: [
        "#1797EB",
        "#F25751",
        "#ffa41e",
        "#9346eb",
        "#02c765",
        "#1797EB",
        "#F25751",
        "#ffa41e",
        "#9346eb",
        "#02c765"
      ]
    };
  },
  methods: {
    goToURL(name, params) {
      this.$router.push({ name, params });
    },
    examine(id) {
      http.getContentDetail({ id }).then(res => {
        this.infoRemark = res.data.remark;
        this.detailInfo = res.data;
        let info = res.data;
        let infoTittle = info.varname;
        let infoContent = JSON.parse(info.varinfo);
        let infoContents = Object.keys(infoContent);
        let allInfo = [];
        if (infoContents.length) {
          for (let info of infoContents) {
            allInfo.push({ value: infoContent[info], label: info });
          }
        }
        for (let type of this.contractChangeStatusList) {
          if (type.value === infoTittle) {
            this.infoTittle = type.label;
          }
        }
        this.infoDetail = allInfo;
        console.log(this.infoDetail);
      });
      this.examineModal = true;
    },
    searchStatus(status) {
      this.status = status;
      this.getListData();
    },
    goToURL(name, query) {
      this.$router.push({ name, query });
    },
    queryListData() {
      this.getListData();
    },
    getListData(
      page = this.currentPage,
      list_rows = this.limit,
      search = this.searchCondition,
      status = this.status
    ) {
      this.loading = true;
      let params = { page, list_rows, search, status };
      if (this.queryDate) {
        params["create_time_start"] = formatDate.toStr(
          new Date(this.queryDate[0]),
          "YYYY-MM-DD"
        );
        params["create_time_end"] = formatDate.toStr(
          new Date(this.queryDate[1]),
          "YYYY-MM-DD"
        );
      }
      let basicConfig = JSON.parse(sessionStorage.getItem("basicConfig"));
      this.nicheStatus = formatArray(basicConfig.opport_status);
      console.log(basicConfig);
      http.getList(params).then(res => {
        this.loading = false;
        this.total = res.data.total;
        let tableListData = res.data.data;
        if (tableListData.length) {
          tableListData.map(item => {
            for (let niche of this.nicheStatus) {
              if (item.opport_status === niche.label.status) {
                item.opport_status = niche.label.label;
              }
            }
            for (let status of this.auditStatusList) {
              if (item.status - status.status === 0) {
                console.log(status);
                item.status = status.label;
              }
            }
          });
        }
        this.tableListData = tableListData;
        for (let row of this.tableListData) {
          for (let status of this.contractChangeStatusList) {
            if (row.varname === status.value) {
              row.varname = status.label;
            }
          }
        }
      });
    },
    confirmDelete() {},
    handleSelectionNiche(val) {
      this.selectionCustomer = val;
      this.selectedNum = this.selectionCustomer.length;
      if (this.selectedNum === 0) {
        this.showBtn = false;
      } else {
        this.showBtn = true;
      }
    },
    confirmArrive() {
      let params = {
        id: this.detailInfo.id,
        status: this.examineForm.status,
        audit_msg: this.examineForm.audit_msg
      };
      http.auditContent(params).then(res => {
        this.$message.success(res.msg || "合同修改审核成功！");
        this.getListData();
        this.examineModal = false;
      });
    },
    init() {
      this.getListData();
      let basicConfig = JSON.parse(sessionStorage.getItem("basicConfig"));
      let contractChangeStatusList = [];
      let keysData = Object.keys(basicConfig.audit_varname);
      if (keysData.length) {
        keysData.map(item => {
          contractChangeStatusList.push({
            value: item,
            label: basicConfig.audit_varname[item]
          });
        });
        this.contractChangeStatusList = contractChangeStatusList;
      }
      let contractStatusList = [];
      let keysData1 = Object.keys(basicConfig.contract_status);
      if (keysData1.length) {
        keysData1.map(item => {
          contractStatusList.push({
            value: item,
            label: basicConfig.contract_status[item].a
          });
        });
        this.contractStatusList = contractStatusList;
      }
      this.auditStatusList = basicConfig.audit_status;

      this.levelList = formatArray(basicConfig.user_level);
      console.log(this.levelList);
    }
  },
  created() {
    this.init();
    http.getExamineStatus().then(res => {
      this.statisticsTagData = res.data;
      this.statisticsTagData.map((item, index) => {
        item.icon = "icon-weifukuan-";
        item.backgroundStart = this.backgroundStart[index];
        item.backgroundEnd = this.backgroundEnd[index];
      });
    });
  }
};
</script>

<style lang="scss" scoped>
.contractChangeApplicationManage {
  padding: 10px;
  .opt_wrap {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 34px;
    &_left {
      display: flex;
      p {
        margin-right: 18px;
        color: #565656;
        font-size: 16px;
        cursor: pointer;
        &:hover {
          color: #1796eb;
        }
        &:nth-of-type(1) {
          cursor: default;
          color: #646464;
          &:hover {
            color: #646464;
          }
        }
      }
    }
    &_right {
      display: flex;
      align-items: center;
      p {
        margin-left: 18px;
        color: #565656;
        font-size: 16px;
        cursor: pointer;
        &:hover {
          color: #1796eb;
        }
      }
      .add {
        width: 72px;
        height: 30px;
        background: linear-gradient(top left, #17b5eb, #1796eb);
        display: flex;
        justify-content: center;
        align-items: center;
        color: white;
        cursor: pointer;
        border-radius: 5px;
        &:hover {
          color: white;
        }
      }
    }
  }
  .search_wrap {
    margin-top: 34px;
    margin-bottom: 34px;
    div {
      margin-right: 17px;
    }
  }
  .see_detail {
    cursor: pointer;
    color: #1796eb;
  }
  .btn {
    margin-top: 30px;
    width: 100%;
    display: flex;
    justify-content: center;
    /deep/ .el-button {
      width: 214px;
    }
  }
  .del_modal {
    .content {
      display: flex;
      flex-direction: column;
      align-items: center;
      p {
        &:nth-of-type(1) {
          width: 92px;
          height: 92px;
          border-radius: 50%;
          background: linear-gradient(to bottom, #f25851, #f27350);
          display: flex;
          justify-content: center;
          align-items: center;
          margin-bottom: 18px;
          i {
            font-size: 40px;
            color: white;
          }
        }
        &:nth-of-type(2) {
          font-size: 20px;
          color: #333;
        }
        &:nth-of-type(3) {
          font-size: 14px;
          color: #999;
          margin-top: 7px;
        }
      }
    }
  }
  .operation {
    cursor: pointer;
    color: rgb(47, 156, 235);
  }
}
</style>
